package cn.zhaozhiqi.template;

import cn.zhaozhiqi.constants.Constants;
import cn.zhaozhiqi.constants.ErrorCode;
import cn.zhaozhiqi.pojo.properties.WeChatIDProperties;
import cn.zhaozhiqi.pojo.vo.WXAccessTokenVo;
import cn.zhaozhiqi.pojo.vo.WXPhoneNumVo;
import cn.zhaozhiqi.pojo.vo.WXRegisterVo;
import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import cn.zhaozhiqi.utils.AssertUtil;

import java.util.HashMap;

public class WeChatTemplate {
    private RestTemplate restTemplate;

    private WeChatIDProperties prop;


    public WeChatTemplate(RestTemplate restTemplate, WeChatIDProperties prop) {
        this.restTemplate = restTemplate;
        this.prop = prop;
    }

    public WeChatTemplate() {
    }

    //写代码，获得Openid
    public WXRegisterVo getOpenid(String code){
        //根据微信平台的api拿到返回的唯一Openid,
        //我们进行封装，将常量提取出来，变量放到yml中
        String url = String.format(Constants.Driver.OPENID_URL,
                prop.getAppId(), prop.getAppSecretId(),code);
        //一般使用getForEntity方法，因为其内封装了返回值的状态码
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        //需要判断状态码
        AssertUtil.isEquals(forEntity.getStatusCode().value(), HttpStatus.OK.value(), ErrorCode.PARAM_CODE_ERROR);
        WXRegisterVo wxRegisterVo = JSON.parseObject(String.valueOf(forEntity.getBody()), WXRegisterVo.class);
        return wxRegisterVo;
    }


    /*获取手机号步骤
    1.获得access_token；
    2.根据access_token发送请求获取手机号
     */

    //获得access_token；
    public WXAccessTokenVo getAccessToken(){
        //我们进行封装，将常量提取出来，变量放到yml中
        String url = String.format(Constants.Driver.ACCESS_TOKEN_URL,
                prop.getAppId(), prop.getAppSecretId());
        //一般使用getForEntity方法，因为其内封装了返回值的状态码
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        //需要判断状态码
        AssertUtil.isEquals(forEntity.getStatusCode().value(), HttpStatus.OK.value(), ErrorCode.PARAM_CODE_ERROR);
        WXAccessTokenVo accessTokenVo = JSON.parseObject(String.valueOf(forEntity.getBody()), WXAccessTokenVo.class);
        return accessTokenVo;
    }

    //根据access_token发送请求获取手机号
    public String getPhoneNum(String phoneCode){
        //拼接路径
        String url = String.format(Constants.Driver.GET_PHONE_URL,
                getAccessToken().getAccess_token());
        //将得到的phonecode放到请求体内
        HashMap<String, String> map = new HashMap<>();
        map.put("code",phoneCode);
        //微信的文档要求：需要使用post方法发送url以及请求体
        ResponseEntity<String> forEntity = restTemplate.postForEntity(url,map,String.class);
        //需要判断状态码
        AssertUtil.isEquals(forEntity.getStatusCode().value(), HttpStatus.OK.value(), ErrorCode.PARAM_CODE_ERROR);
        WXPhoneNumVo phoneNumVo = JSON.parseObject(String.valueOf(forEntity.getBody()), WXPhoneNumVo.class);
        String phoneNumber = phoneNumVo.getPhone_info().getPhoneNumber();
        return phoneNumber;
    }

}
